#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        vector<vector<int>> v;
        for (int i = n - 1; i >= 2; i--) {
            if (nums[i] < 0) {
                break;
            }
            int left = 0;
            int right = i - 1;
            while (left < right) {
                int sum = nums[left] + nums[right];
                if (sum == -nums[i]) {
                    v.push_back({ nums[left],nums[right],nums[i] });
                    left++;
                    right--;
                    while (left < right && nums[left] == nums[left - 1]) {
                        left++;
                    }
                    while (left < right && nums[right] == nums[right + 1]) {
                        right--;
                    }
                }
                else if (sum > -nums[i]) {
                    right--;
                }
                else {
                    left++;
                }
            }
            while (i >= 3 && nums[i] == nums[i - 1]) {
                i--;
            }
        }
        return v;
    }
};
int main()
{
    Solution s;
    vector<int> v;
    v.push_back(-1);
    v.push_back(0);
    v.push_back(1);
    v.push_back(2);
    v.push_back(-1);
    v.push_back(-4);
    vector<vector<int>> V = s.threeSum(v);
    for (auto e : V) {
        for (auto a : e) {
            cout << a << ' ';
        }
        cout << endl;
    }
    int n;
    cin >> n;
	return 0;
}